package com.me.collector;


import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Matrix4;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.esotericsoftware.tablelayout.BaseTableLayout.Debug;
import com.me.collector.Definitions.ItemType;

public class Collector implements ApplicationListener {
	private OrthographicCamera camera;
	private SpriteBatch batch;
	World world;
	Hero hero;
	Ground ground;
	Box2DDebugRenderer debugRenderer;
	Matrix4 debugMatrix;
	Texture bgTexture;
	FallingItems fallingItems;
	ContactLogic contactLogic;

	@Override
	public void create() {
		Texture.setEnforcePotImages(false);
		camera = new OrthographicCamera();
		camera.setToOrtho(false, 800, 480);
		camera.position.set(camera.viewportWidth * .5f, camera.viewportHeight * .5f, 0f);
        camera.update();
		batch = new SpriteBatch();
		world = new World(new Vector2(0, -1.5f), true);
		hero = new Hero(world, 10, 100);
		
		debugMatrix=new Matrix4(camera.combined);
		debugMatrix.scale(Definitions.BOX_TO_WORLD, Definitions.BOX_TO_WORLD, 1f);
		debugRenderer = new Box2DDebugRenderer();
		
		// Test Background
		bgTexture = new Texture(Gdx.files.internal("background/bg1.png"));
		
		// Test Ground
		// BodyDef groundBodyDef =new BodyDef();
		// groundBodyDef.position.set(new Vector2(0, -10));
		// Body groundBody = world.createBody(groundBodyDef);
		// PolygonShape groundBox = new PolygonShape();
		// groundBox.setAsBox(camera.viewportWidth, 10.0f);
		// groundBody.createFixture(groundBox, 0.0f);
		// groundBox.dispose();
		ground = new Ground(world, new Physics(1f, 0.5f, 0.5f, 0f), 20);
        
        // Items
        fallingItems = new FallingItems(world);
        fallingItems.setEndlessSpawn();
        
        // Contact Logic
        contactLogic = new ContactLogic(hero, fallingItems, ground);
        world.setContactListener(contactLogic);
        
        System.out.println("Resolution: " + Gdx.graphics.getWidth() + "x" + Gdx.graphics.getHeight());
	}

	@Override
	public void dispose() {
		hero.dispose();
		batch.dispose();
	}

	@Override
	public void render() {		
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
		//Gdx.gl.glClearColor(1, 1, 1, 1);
		batch.setProjectionMatrix(camera.combined);
		camera.update();
		fallingItems.update();
		ground.update();
		hero.update();

		batch.begin();
		// DRAW
		// Background TEST
		batch.draw(bgTexture, 0, 0);
		fallingItems.draw(batch);
		hero.draw(batch);
		
		batch.end();
		if(Gdx.input.isKeyPressed(Keys.D)) debugRenderer.render(world, debugMatrix);
		if(Gdx.input.isKeyPressed(Keys.S))
		{
			fallingItems.addItem(ItemType.apple);
			fallingItems.addItem(ItemType.cherry);
		}
		if(Gdx.input.isKeyPressed(Keys.R))
		{
			fallingItems.clear();
		}
		if(!Gdx.input.isKeyPressed(Keys.P)) world.step(Definitions.BOX_STEP, Definitions.BOX_VELOCITY_ITERATIONS, Definitions.BOX_POSITION_ITERATIONS);  
	}

	@Override
	public void resize(int width, int height) {
	}

	@Override
	public void pause() {
	}

	@Override
	public void resume() {
	}
}
